package com.myapidemos.apps.app;

import com.example.myapidemos.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MessageServiceActivity extends Activity {
	TextView callback = null;

	class IncomingHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MessageService.MSG_SET_VALUE:
				callback.setText("Received from service: ");
				break;
			case MessageService.MSG_REGISTER_CLIENT:
				callback.setText("Received from MSG_REGISTER_CLIENT: ");
				break;
			case MessageService.MSG_UNREGISTER_CLIENT:
				callback.setText("Received from MSG_UNREGISTER_CLIENT: ");
				break;
			default:
				super.handleMessage(msg);
			}
		}
	}

	final Messenger mMessenger = new Messenger(new IncomingHandler());

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_service);
		callback = (TextView) findViewById(R.id.callback);
		Button bind = (Button) findViewById(R.id.bind);
		bind.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				doBindService();
			}
		});
		Button unbind = (Button) findViewById(R.id.unbind);
		unbind.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doUnBindService();
			}
		});
	}

	boolean mIsBound = false;

	private void doBindService() {
		bindService(new Intent(MessageServiceActivity.this, MessageService.class), connection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
		callback.setText("Binding");
	}

	private void doUnBindService() {
		if (mIsBound) {
			try {
				Message msg = Message.obtain(null, MessageService.MSG_UNREGISTER_CLIENT);
				msg.replyTo = mMessenger;
				mService.send(msg);
			} catch (RemoteException e) {
			}
			unbindService(connection);
			mIsBound = false;
			callback.setText("UnBinding");
		}
	}

	Messenger mService = null;

	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mService = new Messenger(service);
			callback.setText("Attached.");

			try {
				Message msg = Message.obtain(null, MessageService.MSG_REGISTER_CLIENT);
				msg.replyTo = mMessenger;
				mService.send(msg);

				msg = Message.obtain(null, MessageService.MSG_SET_VALUE, this.hashCode(), 0);
				msg.replyTo = mMessenger;
				mService.send(msg);

				Toast.makeText(MessageServiceActivity.this, "service connected", Toast.LENGTH_SHORT)
						.show();
			} catch (RemoteException e) {
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
			callback.setText("Disconnected.");

			Toast.makeText(MessageServiceActivity.this, "service disconnected", Toast.LENGTH_SHORT)
					.show();
		}
	};
}
